const { Router } = require("express");
const User = require("../model/User")
const Update = require("../model/Recipe");
const Works = require("../model/Works");

const router = new Router();
router.use(require('./auths.js'));

// 获取菜谱
router.get("/usermenu", async (req, res) => {
    let { offset, limit } = req.query;
    const user = req.userInfo._id;
    let result = await Update.find({ user_id: user }, { user_id: false, __v: false }).skip(offset).limit(limit);
    const length = await Update.find({ user_id: user }).count()
    res.status(200).json({
        data: result,
        length
    })
})

// 获取作品
router.get("/userworks", async (req, res) => {
    let { offset, limit } = req.query;
    const user = req.userInfo._id;
    const result = await Works.find({ user_id: user }, { user_id: false, __v: false }).skip(offset).limit(limit);
    const length = await Works.find({ user_id: user }).count()
    res.status(200).json({
        data: result,
        length
    })

})



// 修改用户数据
router.patch('/updateInfo', async (req, res) => {
    const user = req.userInfo._id;
    const { avatar, user_name, sex, birthday } = req.body
    console.log(avatar);
    const result = await User.updateOne(
        { _id: user },
        {
            avatar,
            user_name,
            sex,
            birthday
        }
    );
    res.status(200).json(result)
})




module.exports = router;
